package com.yanyu.init.manager;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.*;
import com.yanyu.init.common.ErrorCode;
import com.yanyu.init.config.TencentCosClientConfig;
import com.yanyu.init.exception.BusinessException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Component
public class TencentCosManager {

    @Resource
    private TencentCosClientConfig tencentCosClientConfig;

    @Resource
    private COSClient cosClient;

    /**
     * 上传对象
     *
     * @param key  对象键
     * @param file 文件
     * @return UploadResult上传结果(成功返回 UploadResult, 失败抛出异常)
     */
    public PutObjectResult putObjRequest(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(tencentCosClientConfig.getBucket(), key,
                file);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * @param key           对象键
     * @param localFilePath 本地文件路径
     * @return UploadResult上传结果(成功返回 UploadResult, 失败抛出异常)
     */
    public PutObjectResult putObjectLocalFile(String key, String localFilePath) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(tencentCosClientConfig.getBucket(), key,
                new File(localFilePath));
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 查询对象列表
     *
     * @return 查询对象列表
     */
    public List<String> getCosList() {
        COSClient cosClient = tencentCosClientConfig.cosClient();
        ListObjectsRequest listObjects = new ListObjectsRequest();
        // 设置 bucket 名称
        listObjects.setBucketName(tencentCosClientConfig.getBucket());
        // 设置最大列出多少个对象, 一次 listObject 最大支持1000
        listObjects.setMaxKeys(1000);
        ObjectListing cosLists = cosClient.listObjects(listObjects);
        // 列出的对象列表
        List<COSObjectSummary> summaryList = cosLists.getObjectSummaries();
        if (summaryList == null){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        List<String> cosKey=new ArrayList<>();
        for (COSObjectSummary cosObjectSummary: summaryList){
            cosKey.add(cosObjectSummary.getKey());
        }
        return cosKey;
    }

    /**
     * 删除对象
     *
     * @param key 对象键
     */
    public void deleteObject(String key) {
        COSClient cosClient = tencentCosClientConfig.cosClient();
        if (cosClient.doesObjectExist(tencentCosClientConfig.getBucket(),key)){
            cosClient.deleteObject(tencentCosClientConfig.getBucket(), key);
        }else {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"key不存在");
        }
        cosClient.shutdown();
    }

    /**
     * 生成对象访问 URL
     *
     * @param key 对象键
     * @return 对象访问 URL
     */
    public String getCosUrl(String key) {
        COSClient cosClient = tencentCosClientConfig.cosClient();
        URL url = null;
        try {
            url = cosClient.getObjectUrl(tencentCosClientConfig.getBucket(), key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cosClient.shutdown();
        }
        return String.valueOf(url);
    }
}
